package Model.OtherUser;

import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import Model.Notification;
import DBAccess.DBAccess;

public class OtherUser {

	public static final int FRIEND = 0;
	public static final int BLOCKED = 1;
	protected int friendblocked=-1;
	private int userID;
	private ArrayList<Integer> otherUsers;

	public OtherUser() {
		super();
		update();
	}
	public OtherUser(int userID) {
		super();
		this.userID=userID;
		update();
	}

	public static void addOtherUser(int userID, int friendID, int friendblocked, String group) {
		DBAccess.insertOtherUser(userID, friendID, friendblocked, group);
		if(friendblocked == FRIEND){
			Notification.newNotification(friendID, Notification.friendNotificationMessage(userID),userID);
		}
	}

	public static void removeOtherUser(int userID, int friendID) {
		  DBAccess.removeOtherUser(userID, friendID);
	  }

	public void update(){
		if (userID<0){
			return;
		}
		PreparedStatement st = null;
		ResultSet rs = null;
		  ArrayList<Integer> users = new ArrayList<Integer>();
	  	try {
	  		st = DBAccess.con.prepareStatement("SELECT * FROM otherusersdb WHERE friendblocked=?, userID=?");
	  		st.setInt(1, friendblocked);
	  		st.setInt(2, userID);
	  		rs = st.executeQuery();
	  		while (rs.next()){
	  			users.add(rs.getInt("otheruserID"));
	        }
			rs.close();
	  	} catch (Exception e) {
	  		e.printStackTrace();
	  	}
		  DBAccess.close(st,rs);
	  	otherUsers = users;
	}
	
	public ArrayList<Integer> getOtherUser(int userID, int friendBlocked) {
		return otherUsers;
	  }
	
	public void changeUserStatus(int userID, int otherUserID, int friendblocked){
		DBAccess.updateOtherUser(userID, otherUserID, friendblocked, null);
	}

	  public void addOtherUser(int userID, int friendID, String group){
		  addOtherUser(userID,friendID,friendblocked,group);
	  }
	  public ArrayList<Integer> getOtherUser(int userID){
		  return getOtherUser(userID,friendblocked);
	  }
	  
		protected static boolean isFriendOrBlocked(int userID, int otherID, int status){
			PreparedStatement ps = null;
			ResultSet rs = null;
			boolean value = false;
			try {
				ps = DBAccess.con.prepareStatement("SELECT friendblocked FROM otherusersdb WHERE userID=? AND otherUserID=? AND friendblocked=?");
				ps.setInt(1, userID);
				ps.setInt(2, otherID);
				ps.setInt(3, status);
				rs = ps.executeQuery();
				if(rs.next()){
					value = rs.getInt(1)==status;
					
				}
				
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBAccess.close(ps,rs);
			return value;
		}

}